<?php

include_once('viper/autoloader.php');

class viper_server_groupFeature {
  public static $n = 100;
  public static $m = 50;
  public static $f = 10;
  
  public static function GetGroupRecommendation($users) {
    $n = self::$n;
    $m = self::$m;
    $f = self::$f;
    
    $result = viper_server_dbOps::ReadFromUserFeatureScoreDatabase();
    $friends = viper_server_dbOps::ReadFromUserQuicklist();
    
    $nusers = count($users);
    $total_scores = array();
    for ($idx = 0; $idx < $nusers; $idx++) {
      for ($friend_idx = 0; $friend_idx < $f; $friend_idx++) {
        $cur = $friends[$users[$idx]]['friend'.$friend_idx];
        if ($cur == -1)
          break;
          
        for ($i = 0; $i < $m; $i++) {
          if ($idx + $friend_idx == 0) {
            $total_scores[$i] = $result[$cur]['feature'.$i];
          } else { 
            $total_scores[$i] = $total_scores[$i] + $result[$cur]['feature'.$i];
          }
        }
      }
    }
    
    $best_features = array();
    if ($nusers == 0) {
      $global_average = viper_server_dbOps::ReadFromGlobalAverage();
      for ($i = 0; $i < $m; $i++) {
        $best_features['feature'.$i] = $global_average[0]['feature'.$i];
      }
    } else {
      for ($i = 0; $i < $m; $i++) {
        $total_scores[$i] = $total_scores[$i] / ($nusers * $f);
        $best_features['feature'.$i] = $total_scores[$i];
      }
      
      asort($best_features);
      $best_features = array_reverse($best_features, true);
    }
    
    return $best_features;
  }
}  // End of class viper_server_groupFeature

?>